function amset(rackchans, vals, rates)
%AMSET
%

rackchans=amparserackchan(rackchans);
    
if nargin<3 %if rates not provided
    rates=abs([rackchans.maxrate]); %use max rate of rackchan
end

%cap rate at the max for each channel
rates=abs(rates);
rates=min(rates,abs([rackchans.maxrate]));

%cap values at either minimum or maximum for each channel
vals=min(vals,[rackchans.max]);
vals=max(vals,[rackchans.min]);

%% separate step and ramp channels
channels=[rackchans.channel];
isramp=logical([channels.isramp]);

stepchans=rackchans(~isramp);
stepvals=vals(~isramp);
steprates=rates(~isramp);

rampchans=rackchans(isramp);
rampvals=vals(isramp);
ramprates=rates(isramp);

%% ramp auto channels
rampstart=amget(rampchans);
for i=1:length(rampchans)
    rampchans(i).channel.setfun(rampvals(i)/rampchans(i).multiplier-rampchans(i).const,ramprates(i));
end

%% set step channels
%find current values
curr=zeros(size(stepchans));
for i=1:length(stepchans)
    curr(i)=stepchans(i).channel.getfun();
end

if ~isempty(stepchans)
    start=tic;
    while 1
        now=toc(start);
        newval=zeros(size(stepchans));
        for i=1:length(stepchans)
            newval(i)=now*steprates(i)*sign(stepvals(i)-curr(i))+curr(i);
            %don't go past starting value
            if (newval(i)-stepvals(i))*sign(stepvals(i)-curr(i))>0,newval(i)=stepvals(i);end
            stepchans(i).channel.setfun(newval(i)/stepchans(i).multiplier-stepchans(i).const); 
        end
        %leave when all channels are finished ramping
        if all(newval==stepvals),break,end
    end
end
%% wait for auto channels for finish
while 1
    curr=amget(rampchans);
    %done if channels are within 0.1% of their goal (by total ramp size)
    if all((curr-rampstart)./(rampvals-rampstart)>0.999),break,end
end
end
